package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.base.JsonResult;
import cn.tedu.egweibo1.base.StatusCode;
import cn.tedu.egweibo1.mapper.UserMapper;
import cn.tedu.egweibo1.pojo.dto.UserAddparam;
import cn.tedu.egweibo1.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    @PostMapping("add")

    public JsonResult add(@RequestBody UserAddparam userAddparam){
        log.debug("userAddParam = " + userAddparam );

        User user = new User();
        BeanUtils.copyProperties(userAddparam,user);
        user.setCreateTime(new Date());

        int i = userMapper.add(user);
        if (i>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED,user);
        }
    }
}
